//
// standard initialization routine including interrupt service setup
//

// interrupt routine

#pragma interrupt interrupt_service
void interrupt_service(void)
{
   freeclk_interrupt();      // executed to service freeclk interrupt
}   
   
// set interrupt vector

#pragma code InteruptVector = 0x08 
void InterruptVector(void)
{
_asm
goto interrupt_service
_endasm
}
#pragma code

void initialize(void)
{
   picInit(SYSCLK);   // basic initialization

   TRISD = 0;         // specify LEDs for output

   //  Vex ultrasonic rangefinder
   //  All these require timer T2: must also enable delayT2_init() below
   //  Up to 3 devices can be controlled using different RC or RD pins

//   readVexUltraSonic1_init();   // #1 uses pins RC0, RC1
//   readVexUltraSonic2_init();   // #2 uses pins RD0, RD1
//   readVexUltraSonic3_init();   // #3 uses pins RD2, RD3

   // UART for stdin, stdout access
   // the last param spbrg determines the baud rate = FOSC / (16 * (spbrg + 1))
   // For 20Mhz clk: 10=115200, 21=57600, 32=38400, 64=19200, 129=9600
   // For 40Mhz clk: 10=230400, 21=115200, 42=57600, 32=38400, 64=38400, 129=19200, 259=9600
   
   OpenUSART( USART_TX_INT_OFF  &
              USART_RX_INT_OFF  &
              USART_ASYNCH_MODE &
              USART_EIGHT_BIT   &
              USART_CONT_RX     &
              USART_BRGH_HIGH,
              10              );      // sets for 115200 BPS @ 20MHz clk

   // I2C peipheral for output D/A, eg for Vex motors
   
//   OpenI2C(MASTER, SLEW_ON);         // enables I2C device
//  Set 400KHz I2C clk by setting SSPADD = (FOSC / 1,600,000) - 1 
   SSPADD = 12;                  // 400KHz I2C Clk for 20MHz FOSC, see Data Sheet SSPADD sec
   SSPADD = 24;                  // 400KHz I2C Clk for 40MHz FOSC, see Data Sheet SSPADD sec

   // Freerunning clock uses timer T0, including interrupt
      
//   INTCONbits.GIE = 1;               // Global Enable Interrupt
//   freeclk_init();
   
   // Timers for delayTx_ms(ms) and delayTx_us(us)
   
//   delayT0_init();                 // enables timer 0 for delays
//   delayT1_init();                 // enables timer 1 for delays
//   delayT2_init();                 // enables timer 2 for delays
//   delayT3_init();                 // enables timer 3 for delays
   
}

